package com.ornament.app.domain;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class ImageDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<Image> data;

	@Autowired
    private AlbumDataOnDemand albumDataOnDemand;

	public Image getNewTransientImage(int index) {
        Image obj = new Image();
        setAlbum(obj, index);
        setImageFile(obj, index);
        setName(obj, index);
        return obj;
    }

	public void setAlbum(Image obj, int index) {
        Album album = albumDataOnDemand.getRandomAlbum();
        obj.setAlbum(album);
    }

	public void setImageFile(Image obj, int index) {
        byte[] imageFile = String.valueOf(index).getBytes();
        obj.setImageFile(imageFile);
    }

	public void setName(Image obj, int index) {
        String name = "name_" + index;
        if (name.length() > 45) {
            name = new Random().nextInt(10) + name.substring(1, 45);
        }
        obj.setName(name);
    }

	public Image getSpecificImage(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        Image obj = data.get(index);
        Integer id = obj.getId();
        return Image.findImage(id);
    }

	public Image getRandomImage() {
        init();
        Image obj = data.get(rnd.nextInt(data.size()));
        Integer id = obj.getId();
        return Image.findImage(id);
    }

	public boolean modifyImage(Image obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = Image.findImageEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'Image' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<Image>();
        for (int i = 0; i < 10; i++) {
            Image obj = getNewTransientImage(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
